package src.Fourteen;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class Item extends JFrame {

    public Item(){
        JComboBox comboBox = new JComboBox();
        for (int i = 0; i < 6; i++) {
            comboBox.addItem("选项"+i);
        }
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                int stateChange = e.getStateChange();
                String item = e.getItem().toString();
                if(stateChange == ItemEvent.SELECTED){
                    System.out.println("此次事件由选中选项"+item+"触发！");
                } else if (stateChange == ItemEvent.DESELECTED) {
                    System.out.println("此次事件由取消选中选项"+item+"触发！");
                }else {
                    System.out.println("此次事件由其他原因触发！");
                }
            }
        });

        Container container = getContentPane();
        container.add(comboBox);
        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Item item = new Item();
    }
}
